﻿using System.Data;
using JKF.BusinessLayer.BLL;
using JKF.Entities;
using JKF.Entities.Model;
using JKF.Substructure;
using SFWS.XHandler.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JKF.HandlerLayer.Other
{
    public class SWING_CARDHandler : AHttpHandler
    {
        private static bllSWING_CARD bllSwingCard = new bllSWING_CARD();
        public override void ProcessRequest()
        {
            try
            {
                this.HandleRequest(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                AppLogger.Log(ex.Message, ex);  //写日志
            }
        }

        public void GetRemindInfo()
        {
            var remindType = P("remindType");

            Response.Write(JsonSerializer.Serialize(bllSwingCard.GetRemindInfo(remindType)));
        }

        public void GetSwingCard()
        {
          
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;

            //P("isExport") == "1" 为导出excel,将pageSize设置为-1,表示显示全部
            if (P("isExport") == "1")
            {
                DataTable dt = bllSwingCard.GetSwingCard(P("searchItem"), P("matchWay"), P("keyword"), P("dateStart"), P("dateEnd"), P("ownShop"), P("PageIndex").ToInt32(), -1, ref pageCount, ref rowCount);

                IDictionary<string, string> columns = new Dictionary<string, string>();
                columns.Add("CARD_NO", "卡号");
                columns.Add("NAME", "姓名");
                columns.Add("MOBILE", "手机号");
                columns.Add("TYPE", "录入类型");
                columns.Add("TRY_TIME", "体验时间");
                columns.Add("POINT_VALUE", "本次积分");
                columns.Add("ACCOUNT", "操作员");
                Excel.Export("体验记录", dt, columns, Response, Excel.PageType.handler);

            }
            else
            {
                obj.Entity = from a in bllSwingCard.GetSwingCard(P("searchItem"), P("matchWay"), P("keyword"), P("dateStart"), P("dateEnd"), P("ownShop"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                             select new
                             {
                                 ID = a.Field<Guid>("ID").ToString(),
                                 CARD_NO = a.Field<string>("CARD_NO"),
                                 NAME = a.Field<string>("NAME"),
                                 MOBILE = a.Field<string>("MOBILE"),
                                 TYPE = a.Field<string>("TYPE"),
                                 TRY_TIME = a.Field<DateTime>("TRY_TIME").ToString(),
                                 POINT_VALUE = a.Field<Int32>("POINT_VALUE").ToString(),
                                 TOTAL_POINT = bllSwingCard.GetTotalPoint(a.Field<Guid>("CUSTOMER_KEY").ToString()),
                                 ACCOUNT = a.Field<string>("ACCOUNT")
                             };

                obj.RowCount = rowCount;
                obj.PageCount = pageCount;
                ResponseJsonObject = obj;
            }
        }

        /// <summary>
        /// 获取顾客换卡记录日志
        /// </summary>
        [Exp2Client]
        public void GetCustomerSwingCardLog()
        {
            var customerKey = P("customerKey");
            if (!customerKey.IsGuidB()) throw new Exception("顾客编号不正确.");
            var rslt = bllSWING_CARD.GetCustomerCardChangeLog(customerKey, bllSYS_USER.CurrentUser.USER_KEY.ToString());
            Response.Write(new ResponseInf
                {
                    Success = true,
                    Data = (from t in rslt.AsEnumerable()
                            select new
                                {
                                    ID = t.Field<Guid>("ID"),
                                    CUSTOMER_KEY = t.Field<Guid>("CUSTOMER_KEY"),
                                    OLD_LEVEL_NAME = t.Field<string>("OLD_LEVEL_NAME"),
                                    OLD_CARD = t.Field<string>("OLD_CARD"),
                                    NEW_CARD = t.Field<string>("NEW_CARD"),
                                    DATE_CHANGE = t.Field<DateTime?>("DATE_CHANGE").ToShortDateLineFormat(),
                                    DATE_CREATE = t.Field<DateTime?>("DATE_CREATE").ToShortDateLineFormat(),
                                    CUSTOMER_NAME = t.Field<string>("CUSTOMER_NAME"),
                                    NEW_LEVEL_NAME = t.Field<string>("NEW_LEVEL_NAME"),
                                    OPERATOR = t.Field<string>("OPERATOR")
                                }).ToList()
                }.ToJson());
        }

        public void DeleteSwingCard()
        {
            try
            {
                var pk = P("pk");
                bllSwingCard.DeleteSwingCard(pk);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public void SaveSwingCard()
        {
            try
            {
                bllSWING_CARD bllSwingCard = new bllSWING_CARD();
                var customerKey = P("customerKey");
                var tryType = P("tryType");

                bool isHas= bllSwingCard.IsHasSwingCardRecord(customerKey);
                if (isHas)
                {
                    Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = "此人今天已添加过体验记录，不可再添加!", Data = "" }));
                    return;
                }

                var pk = bllSwingCard.SaveSwingCard(customerKey, tryType);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "体验记录添加成功.", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public void GetSwingSequenceByShopCode()
        {  
            try
            {
                var shopCode = P("shopCode");
                if (shopCode == "")
                    shopCode = bllSYS_USER.CurrentUser.SHOP_CODE;
                Response.Write(JsonSerializer.Serialize(bllSwingCard.GetSwingSequenceByShopCode(shopCode), Utils.GetDateTimeFormat));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
    }
}
